package com.bigshen.algorithm.mTypeHead.solution01Trie;

import java.util.HashMap;
import java.util.Map;


/**
 * 208. Implement Trie (Prefix Tree) 前缀树
 * Implement a trie with insert, search, and startsWith methods.
 *
 * Example:
 *
 * Trie trie = new Trie();
 *
 * trie.insert("apple");
 * trie.search("apple");   // returns true
 * trie.search("app");     // returns false
 * trie.startsWith("app"); // returns true
 * trie.insert("app");
 * trie.search("app");     // returns true
 * Note:
 *
 * You may assume that all inputs are consist of lowercase letters a-z.
 * All inputs are guaranteed to be non-empty strings.
 *
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/implement-trie-prefix-tree
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 */
public class Trie {

    class TrieNode {
        char val;
        Map<Character, TrieNode> children;
        boolean hasWord;

        public TrieNode() {
            children = new HashMap<>();
        }

        public TrieNode(char val) {
            this.val = val;
            children = new HashMap();
        }
    }

    TrieNode root;

    /** Initialize your data structure here. */
    public Trie() {
        root = new TrieNode();
    }

    /** Inserts a word into the trie. */
    public void insert(String word) {
        char[] array = word.toCharArray();
        TrieNode current = root;
        Map<Character, TrieNode> children = current.children;

        for (int i = 0; i < array.length; i ++) {
            Character element = array[i];
            if (!children.containsKey(element)) {
                // 不包含当前元素，将当前元素初始化为子节点，放入树中
                TrieNode node = new TrieNode(element);
                children.put(element, node);
                current = node;
            } else {
                current = children.get(element);
            }
            children = current.children;
        }
        // 最后，元素为单词
        current.hasWord = true;
    }

    /** Returns if the word is in the trie. */
    public boolean search(String word) {
        char[] array = word.toCharArray();
        TrieNode current = root;
        Map<Character, TrieNode> children = current.children;

        for (int i = 0; i < array.length;  i ++) {
            Character element = array[i];
            if (!children.containsKey(element)) {
                // 树中不包含当前元素，继续判断
                return false;
            }
            current = children.get(element);
            children = current.children;
        }
        // 能遍历至最后，返回末尾节点的 hasWord
        return current.hasWord;
    }

    /** Returns if there is any word in the trie that starts with the given prefix. */
    public boolean startsWith(String prefix) {
        char[] array = prefix.toCharArray();
        TrieNode current = root;
        Map<Character, TrieNode> children = current.children;

        for (int i = 0; i < array.length;  i ++) {
            Character element = array[i];
            if (!children.containsKey(element)) {
                return false;
            }
            current = children.get(element);
            children = current.children;
        }
        // 包含即可，不一定是单词
        return true;
    }

}

/**
 * Your Trie object will be instantiated and called as such:
 * Trie obj = new Trie();
 * obj.insert(word);
 * boolean param_2 = obj.search(word);
 * boolean param_3 = obj.startsWith(prefix);
 */
